package com.timoon.component.panel.tree.treetable;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Alignment;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Unit;
import org.apache.wicket.extensions.markup.html.tree.table.IColumn;
import org.apache.wicket.extensions.markup.html.tree.table.PropertyRenderableColumn;
import org.apache.wicket.extensions.markup.html.tree.table.TreeTable;
import org.apache.wicket.markup.html.panel.Panel;

import com.timoon.component.panel.treetable.attachment.CheckBoxPropertyColumn;

/**
 * treeTable 三列，第一个是复选框,且取值约定为id属性
 * @author Administrator
 *
 */
public class ThreeColumnTabelPanel extends Panel {
	public static final long serialVersionUID = -2300676765628732744L;
	public static String COLUMN1_TITLE_CONFIGMAP_KEY="column1-title";
	public static String COLUMN1_EXPRESSION_CONFIGMAP_KEY="column1-exp";
	public static String COLUMN2_TITLE_CONFIGMAP_KEY="column2-title";
	public static String COLUMN2_EXPRESSION_CONFIGMAP_KEY="column2-exp";
	
	final CheckBoxPropertyColumn checkboxColumn;
	
	/**
	 * 
	 * @param id
	 * @param data	条目对象集合，若没有覆盖getColumn1，没有将第一列变为tree的话，数据不需要父子关系
	 * @param checkedIds 初始化时已选中的记录id集合
	 * @param configMap	 参数配置，具体看下面定义的静态key
	 */
	public ThreeColumnTabelPanel(String id,List<Object> data,List<String> checkedIds,Map<String,String> configMap) {
		super(id);
		
		
		checkboxColumn=new CheckBoxPropertyColumn(new ColumnLocation(Alignment.LEFT,20,Unit.PERCENT),"启用","userObject.id",checkedIds);
		IColumn column1=this.getColumn1(new ColumnLocation(Alignment.LEFT,40,Unit.PERCENT),configMap.get(COLUMN1_TITLE_CONFIGMAP_KEY), "userObject."+configMap.get(COLUMN1_EXPRESSION_CONFIGMAP_KEY));
		IColumn column2=new PropertyRenderableColumn<>(new ColumnLocation(Alignment.LEFT,40,Unit.PERCENT), configMap.get(COLUMN2_TITLE_CONFIGMAP_KEY), "userObject."+configMap.get(COLUMN2_EXPRESSION_CONFIGMAP_KEY));
		
		IColumn[] columns=new IColumn[]{checkboxColumn,column1,column2};
		
		@SuppressWarnings("deprecation")
		TreeTable treeTable=new TreeTable("wk_treetable",buildTreeModel(data),columns){
			@Override
			protected void onNodeLinkClicked(AjaxRequestTarget target,
					TreeNode node) {
				// TODO Auto-generated method stub
			}
		};
		treeTable.getTreeState().expandAll();
		add(treeTable);
		
	}
	
	protected TreeModel buildTreeModel(List<Object> data){
		DefaultMutableTreeNode root=null;
		
		if(data!=null){
			if(data.size()==1){
				root=new DefaultMutableTreeNode(data.get(0));
			}else if(data.size()>1){
				root=new DefaultMutableTreeNode(data.get(0));
				
				for(int i=1;i<data.size();i++){
					DefaultMutableTreeNode node=new DefaultMutableTreeNode(data.get(i));
					root.add(node);
				}
			}
		}
		return new  DefaultTreeModel(root);
	}
	/**
	 * 第二列从这里获取，提供方法来获取的原因是方便子类覆盖此方法，便可更改此列
	 * 注意：若覆盖此方法，将第二列编程tree的话， 也同时需要覆盖buildTreeModel()<br>
	 * 因为当前返回的模型不使用与tree(非嵌套关系)
	 * @param configMap
	 * @return
	 */
	protected IColumn getColumn1(ColumnLocation location,String title,String expression){
		return new PropertyRenderableColumn<>(location,title,expression);
	}
	/**
	 * 获取用户选中的id
	 * @return
	 */
	public Set<String> getSelectedIds(){
		
		return checkboxColumn.getCheckedIds();
	}
}
